package com.qf.flash.ui.circle;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.qf.flash.R;
import com.qf.flash.base.BaseRefreshFragment;
import com.qf.flash.model.HotListBean;
import com.qf.flash.mvp.contract.HotlistContract;
import com.qf.flash.mvp.presenter.HotListBeanPresenter;
import com.qf.flash.ui.circle.adapter.HotListItem;
import com.qf.flash.ui.home.ScriptDetailActivity;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.widget.ViewUtil;

import java.util.List;

/**
 * 圈子模块  ---->  推荐
 */
public class HotFragment extends BaseRefreshFragment implements HotlistContract.View {


    private RVAdapter<HotListBean> mAdapter;
    private String type;
    private int pageNo = 1, pagesize = 30;
    private HotListBeanPresenter hotListPresenter;

    @SuppressLint({"RestrictedApi", "ResourceAsColor"})
    @Override
    protected void lazyLoad() {
        type = getArguments().getString("type");
        mRefresh.setBackgroundColor(getResources().getColor(R.color.color_hot_bg));
        freshHeader.setBackgroundColor(getResources().getColor(R.color.color_hot_bg));
        mRefresh.autoRefresh();
    }

    @Override
    protected void requestData(boolean isRefresh, int page) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        hotListPresenter.getHotListDatas(type, page, pagesize);
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    @Override
    public RecyclerView.Adapter getRecyclerAdapter() {
        mAdapter = new RVAdapter<>(new HotListItem());
        mAdapter.setOnItemClickListener(new RVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ScriptDetailActivity.start(getActivity(), mAdapter.getData(position).getId());
            }
        });
        return mAdapter;
    }

    @Override
    protected void initInject() {
        super.initInject();
        hotListPresenter = new HotListBeanPresenter();
        initPresenters(hotListPresenter);
    }


    public static HotFragment newInstance(String type) {
        HotFragment recommendFragment = new HotFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        recommendFragment.setArguments(bundle);
        return recommendFragment;
    }

    @Override
    public void setHotListBeans(List<HotListBean> listBeans, int totalPage) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        mRefresh.setEnableLoadMore(page < totalPage);
        List<HotListBean> datas = mAdapter.getDatas();
        if (isRefresh) {
            datas.clear();
        }
        datas.addAll(listBeans);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onError(ApiException e) {
        super.onError(e);
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
    }
}
